const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: "",
    baseUrl: Api.base_url
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let flowList = JSON.parse(options.flowList)
    let state = options.state
    let title = "当前挂号"
    console.log("state: ", state)

    if(state == -1){
      title = "历史挂号"
    }
    console.log("flowList: ", flowList)
    this.setData({
      flowList: flowList,
      title: title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  toDetails: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },
  toEndFlow: function(e) {
    var self = this
    let state = e.currentTarget.dataset.state
    let flowId = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    // 判断是否是正在进行的治疗
    if(state ==1) {
      wx.showModal({
        title: '提示',
        content: '确认结束治疗？',
        success(res) {
          if (res.confirm) {
            var url = Api.flow_shutdown
            var params = {
              flowId: flowId,
            };
            var method = 'POST';
            var header = "application/json";
            var success = function (res) {
              if(res.code == 200) {
                wx.showToast({
                  title: '已结束治疗',
                })
                console.log("中断流程，结束治疗: ", res)
                // 去除删除对象
                let flowList = self.data.flowList
                flowList.splice(index, 1)
                self.setData({
                  flowList: flowList
                })
              } else{
                wx.showToast({
                  icon: 'loading',
                  title: res.msg,
                })
              }
              
            }
            // ApiRequest.request(url, params, method, header, success, self.fail);
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  }
})